package com.mask.statistics.controller;



import com.mask.roll.common.code.ServiceEnums;
import com.mask.roll.common.constant.RollConstants;
import com.mask.roll.common.util.RollResponse;
import com.mask.statistics.service.StatisticsDailyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 网站统计日数据 前端控制器
 * </p>
 *
 * @author hx
 */
@RestController
@RequestMapping("/statistics")
@Slf4j
@Api(tags = "统计日数据")
public class StatisticsDailyController {

    @Autowired
    private StatisticsDailyService staService;

    /**
     * 统计某一天注册人数,生成统计数据
     * @param day
     * @return
     */
    @PostMapping("/count/register/{day}")
    @ApiOperation("统计一天中注册人数")
    public RollResponse registerCount(@ApiParam("注册日期具体某一天") @PathVariable String day) {
        try {
            staService.registerCount(day);
            log.info("生成数据{}注册人数成功",day);
            return RollResponse.result(ServiceEnums.STATISTICS_GENERATE_COUNT_REGISTER_SUCCESS.getCode(),
                    RollConstants.SUCCESS,ServiceEnums.STATISTICS_GENERATE_COUNT_REGISTER_SUCCESS.getMessage());
        } catch (Exception e) {
            log.warn("生成数据{}注册人数失败:{}",day,e.getMessage());
            return RollResponse.result(ServiceEnums.STATISTICS_GENERATE_COUNT_REGISTER_FAILED.getCode(),
                    RollConstants.ERROR,ServiceEnums.STATISTICS_GENERATE_COUNT_REGISTER_FAILED.getMessage());
        }

    }

    /**
     * 图表显示，返回两部分数据，日期json数组，数量json数组
     * @param type 类型
     * @param begin 开始时间
     * @param end 结束时间
     * @return
     */
    @GetMapping("/show/data/{type}/{begin}/{end}")
    public RollResponse showData(@PathVariable String type,@PathVariable String begin,
                      @PathVariable String end) {
        if (StringUtils.isEmpty(type) || StringUtils.isEmpty(begin) || StringUtils.isEmpty(end)){
            return RollResponse.result(ServiceEnums.STATISTICS_ECHART_QUERY_NULL.getCode(),
                    RollConstants.ERROR,ServiceEnums.STATISTICS_ECHART_QUERY_NULL.getMessage());
        }
        Map<String,Object> map = null;
        try {
            map = staService.getShowData(type,begin,end);
            log.info("图表查询成功:{}",map);
            return RollResponse.result(ServiceEnums.STATISTICS_ECHART_SHOW_SUCCESS.getCode(),
                    RollConstants.SUCCESS,ServiceEnums.STATISTICS_ECHART_SHOW_SUCCESS.getMessage(),map);
        } catch (Exception e) {
            log.error("图表查询失败:{}",e.getMessage());
            return RollResponse.result(ServiceEnums.STATISTICS_ECHART_SHOW_FAILED.getCode(),
                    RollConstants.ERROR,ServiceEnums.STATISTICS_ECHART_SHOW_FAILED.getMessage(),map);
        }

    }
}

